package com.dfbz.demo02_线程安全测试;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
public class Demo04_静态同步方法 {
    public static void main(String[] args) {
        Print print = new Print();
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    print.print1();
                }
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    print.print2();
                }
            }
        }.start();
    }
}

class Print{

    // 静态同步方法的锁对象是当前类的字节码对象
    public static synchronized void print1() {
        System.out.print("犯");
        System.out.print("我");
        System.out.print("中");
        System.out.print("华");
        System.out.print("者");
        System.out.println();
    }

    public void print2() {
        synchronized (Print.class) {
            System.out.print("虽");
            System.out.print("远");
            System.out.print("必");
            System.out.print("诛");
            System.out.println();
        }
    }
}
